/*
 * Copyright (c) 2009, ConnectCode Pte Ltd
 *                     http://www.webextractor360.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of ConnectCode nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ConnectCode Pte Ltd ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL ConnectCode Pte Ltd BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using WebExtractor360.look4me.Framework;
using WebExtractor360.look4me.Engine;
using WebExtractor360.look4me.Model;
using WebExtractor360.look4me.Util;

namespace WebExtractor360.look4me.Action
{
    public class SearchAction : WebExtractor360.look4me.Framework.Action
    {
        public static int EXTRACTORACTION = 0;
        private int m_internalAction;

        public SearchAction()
        {
            m_internalAction = EXTRACTORACTION;
            counturi = 0;
        }

        public SearchAction(int actionValue)
        {
            m_internalAction = actionValue;
            counturi = 0;
        }


        private Uri m_actualUri;
        private string[] m_uris;
        public string[] Uris
        {
            get
            {
                return m_uris;
            }
            set
            {
                m_uris = value;
            }
        }

        private string[] m_includeLinks;
        public string[] IncludeLinks
        {
            get
            {
                return m_includeLinks;
            }
            set
            {
                m_includeLinks = value;

            }
        }

        private string[] m_excludeLinks;
        public string[] ExcludeLinks
        {
            get
            {
                return m_excludeLinks;
            }
            set
            {
                m_excludeLinks = value;

            }
        }

        private string[] m_expressions;
        public string[] Expressions
        {
            get
            {
                return m_expressions;
            }
            set
            {
                m_expressions = value;

            }
        }

        private string m_uri;
        public string Uri
        {
            get
            {
                return m_uri;
            }
            set
            {
                m_uri = value;
            }
        }

        private string m_baseuri;
        public string BaseUri
        {
            get
            {
                return m_baseuri;
            }
            set
            {
                m_baseuri = value;
            }
        }

        private int counturi;
        private int m_maxuri;
        public int MaxURI
        {
            get
            {
                return m_maxuri;
            }
            set
            {
                m_maxuri = value;
            }
        }

        private ArrayList m_result;
        public ArrayList GetResult()
        {
            return m_result;
        }

        protected override void doExecute()
        {
            if (m_internalAction == EXTRACTORACTION)
            {
                m_result = new ArrayList();
                m_actualUri = new Uri(m_uri);
                ExtractorProcessingEngine epc = new ExtractorProcessingEngine(m_actualUri, m_maxuri,Expressions, this, m_worker, m_workerevent);
                epc.Execute();
                FilterResults(epc);

                if (m_worker.CancellationPending)
                {
                    m_workerevent.Cancel = true;
                    return;
                }

                m_workerevent.Result = "Success";
                m_worker.ReportProgress(100);
            }
        }

        private void FilterResults(ExtractorProcessingEngine spider)
        {
            IEnumerator ie = spider.GetResults();
            while (ie.MoveNext())
            {
                    if (!WebExtractor360.look4me.Util.ResultsFunction.Contains(m_result, ((ResultsValueObject)(ie.Current)).Result))
                        m_result.Add(((ResultsValueObject)(ie.Current)));
            }
        }

        public override void CommonCallBack(ValueObject vo)
        {
            ExtractorCurrentResultsValueObject svo= (ExtractorCurrentResultsValueObject) vo;

            if (svo.NoProgress == 1)
            {
                ((WorkerForm)m_form).CommonCallBack(vo);
            }
            else
            {
                if (m_internalAction == EXTRACTORACTION
                    )
                {
                    m_workerevent.Result = "Processing";
                    counturi++;
                    int progresspercent = (int)(((counturi * 1.0) / m_maxuri) * 100);
                    if (progresspercent > 99)
                        progresspercent = 99;
                    m_worker.ReportProgress(progresspercent);

                    ((WorkerForm)m_form).CommonCallBack(vo);
                }
                //the reporting here ignore the spider reporting. instead it reports right here
            }
        }
    }
}
